/* 
 * Copyright 2014 Josef Hertl.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package caetus.pkcs11.session;

import java.util.LinkedList;
import java.util.List;

/**
 * This class serves to sort the output of the various functions and to make
 * sorting through the results easier.
 *
 * @author Josef Hertl
 */
public class OutputLogger {
    private List<OutputMessage> outputList = new LinkedList<OutputMessage>();
    private boolean showFullResults;

    public OutputLogger(boolean showSuccess) {
        this.showFullResults = showSuccess;
    }

    /**
     * Marks the test as successful.
     *
     * @param symmetric If a symmetric key was used.
     * @param functionName The name of the function.
     */
    public void addSuccess(boolean symmetric, String functionName) {
        outputList.add(new OutputMessage(symmetric, functionName, true, 0, ""));
    }

    /**
     * Marks the test as successful, but permits adding a comment.
     *
     * @param symmetric If a symmetric key was used.
     * @param functionName The name of the function.
     * @param comment The comment for the function.
     */
    public void addSuccess(boolean symmetric, String functionName, String comment) {
        outputList.add(new OutputMessage(symmetric, functionName, true, 0, comment));
    }

    /**
     * Marks the test as failed and defines severity of the failure.
     *
     * @param symmetric True if a symmetric key was used.
     * @param functionName The name of the function that failed.
     * @param severity Ranges from 1 (least dangerous) to 5 (extremely
     * dangerous)
     * @param comment Why the function failed and what could it mean.
     */
    public void addFailureWithComment(boolean symmetric, String functionName, int severity, String comment) {
        outputList.add(new OutputMessage(symmetric, functionName, false, severity, comment));
    }

    /**
     * @return Easily readable list of function test results.
     */
    public String getResultStrings() {
        String result = "";
        for (OutputMessage outputMessage : outputList) {
            if (showFullResults || (!outputMessage.isSuccess())) {
                if (outputMessage.isSymmetric()) {
                    result += "Symmetric ";
                } else {
                    result += "Asymmetric ";
                }
                result += outputMessage.getFunctionName() + ": ";
                if (outputMessage.isSuccess()) {
                    result += "SUCCESS.";
                } else {
                    result += "FAILURE (" + outputMessage.getSeverity() + ").";
                }
                if (outputMessage.getComment() != null && !outputMessage.getComment().isEmpty()) {
                    result += " " + outputMessage.getComment();
                }
                result += "\n";
            }
        }
        return result;
    }
}
